package net.ck.hzq.presenter;

import net.ck.hzq.view.PosRecordView;

import java.util.List;

import Utils.ActivityUtil;
import Utils.HelloWordModel;
import Utils.SharedPrefStore;
import Utils.ToastUtil;
import dao.BaseOutput;
import dao.FlowDevicesInput;
import dao.GetUrlOutput;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PosRecordPresenter {

    private final PosRecordView iView;

    public PosRecordPresenter(PosRecordView view) {
        iView = view;
    }

    public void queryRule() {
        iView.showProgress();
        HelloWordModel.getInstance(iView.getHostActivity()).getFlowRule().enqueue(new Callback<BaseOutput<GetUrlOutput>>() {
            @Override
            public void onResponse(Call<BaseOutput<GetUrlOutput>> call, Response<BaseOutput<GetUrlOutput>> response) {
              try{
                if (ActivityUtil.isActivityDestroyed(iView.getHostActivity())) {
                    return;
                }
                iView.hideProgress();
                BaseOutput<GetUrlOutput> body = response.body();
                if (body == null) {
                    return;
                }
                if ("success".equals(body.msg) && body.data != null) {
                    if (body.data.transfer != null) {
                        iView.showFlowRuleDialog(body.data.transfer);
                    }
                }
              } catch (Exception e) {
                  e.printStackTrace();
              }
            }

            @Override
            public void onFailure(Call<BaseOutput<GetUrlOutput>> call, Throwable t) {
                if (ActivityUtil.isActivityDestroyed(iView.getHostActivity())) {
                    return;
                }
                iView.hideProgress();
            }
        });
    }

    public void flowDevices(Integer toId, List<String> noList, int backAmount) {
        SharedPrefStore store = new SharedPrefStore();
        int id = Integer.valueOf(store.getString("id", "0"));
        FlowDevicesInput input = new FlowDevicesInput();
        input.fromId = id;
        input.toId = toId;
        input.snList = new String[noList.size()];
        input.backAmount = backAmount;
        for (int i = 0; i < noList.size(); i++) {
            input.snList[i] = noList.get(i);
        }
        iView.showProgress();
        HelloWordModel.getInstance(iView.getHostActivity()).flowDevices(input).enqueue(new Callback<BaseOutput<Object>>() {
            @Override
            public void onResponse(Call<BaseOutput<Object>> call, Response<BaseOutput<Object>> response) {
               try{
                if (ActivityUtil.isActivityDestroyed(iView.getHostActivity())) {
                    return;
                }
                iView.hideProgress();
                BaseOutput<Object> body = response.body();
                if (body == null) {
                    return;
                }
                if ("success".equals(body.msg) && body.data != null) {
                    iView.updateDevices();
                } else {
                    ToastUtil.showNormal(iView.getHostActivity(), body.msg);
                }
               } catch (Exception e) {
                   e.printStackTrace();
               }
            }

            @Override
            public void onFailure(Call<BaseOutput<Object>> call, Throwable t) {
                if (ActivityUtil.isActivityDestroyed(iView.getHostActivity())) {
                    return;
                }
                iView.hideProgress();
            }
        });
    }

}
